package me.khrystal.bdlocation;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

public class MainActivity extends AppCompatActivity {

    TextView textLocation;
    private LocationClient locationClient = null;
    public static final int UPDATE_TIME = 5000;
    private static int LocationCounts = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initLocation();

        textLocation = (TextView)findViewById(R.id.text_location);
        findViewById(R.id.btn_location).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (locationClient != null) {
                    if (locationClient.isStarted()) {
                        ((Button) v).setText("Start");
                        locationClient.stop();
                    }else {
                        ((Button)v).setText("Stop");
                        locationClient.start();
                        locationClient.requestLocation();
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (locationClient != null && locationClient.isStarted()) {
            locationClient.stop();
            locationClient = null;
        }
    }

    private void initLocation() {
        locationClient = new LocationClient(this);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);
        option.setCoorType("bd09ll");
        option.setPriority(LocationClientOption.NetWorkFirst);
        option.setProdName("LocationDemo");
        option.setScanSpan(UPDATE_TIME);
        locationClient.setLocOption(option);
        locationClient.registerLocationListener(new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {
                if (bdLocation==null) return;
                StringBuffer sb = new StringBuffer(256);
                sb.append("Time :"+bdLocation.getTime()).append("\nErrorCode : "+bdLocation.getLocType())
                        .append("\nLatitute :" + bdLocation.getLatitude()).append("\nLontitute :"+bdLocation.getLongitude())
                        .append("\nRadius :"+bdLocation.getRadius())
                        .append(bdLocation.getLocType()==BDLocation.TypeGpsLocation ? "\nSpeed :"+bdLocation.getSpeed()+"\nSatellite :"+bdLocation.getSatelliteNumber():
                        "\nAddress :"+bdLocation.getAddrStr());
                LocationCounts++;
                sb.append("\n检查更新次数 :"+LocationCounts);

                textLocation.setText(sb.toString());
            }
        });
    }
}
